﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Serializer = YamlDotNet.Serialization.Serializer;

namespace YJTTool.Common.Helper
{
    public static class JsonConverterHelper
    {
        private static readonly Serializer serializer = (Serializer)new SerializerBuilder()
    .WithNamingConvention(CamelCaseNamingConvention.Instance)
    .Build();
        public static string FormatJson(string json)
        {
            var obj = JToken.Parse(json);
            return obj.ToString(Formatting.Indented);
        }

        public static string MinifyJson(string json)
        {
            var obj = JToken.Parse(json);
            return obj.ToString(Formatting.None);
        }

        public static bool ValidateJson(string json)
        {
            try
            {
                JToken.Parse(json);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static string JsonToYaml(string json)
        {
            var obj = JToken.Parse(json);
            return serializer.Serialize(obj);
        }
        public static string JsonToXml(string json)
        {
            var xmlNode = JsonConvert.DeserializeXmlNode(json, "root");
            return xmlNode.OuterXml;
        }
    }
}
